//---------------------------------------------------------------------
//  This file is part of the WindowsWorkflow.NET web site samples.
// 
//  Copyright (C) Microsoft Corporation.  All rights reserved.
// 
//  This source code is intended only as a supplement to Microsoft
//  Development Tools and/or on-line documentation.  See these other
//  materials for detailed information regarding Microsoft code samples.
// 
//  THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//---------------------------------------------------------------------

namespace Crab.WorkflowClient
{
    #region Using Statements

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Collections.Generic;
    using System.ComponentModel.Design;
    using System.Workflow.ComponentModel;
    using System.Workflow.ComponentModel.Design;
    using System.Windows.Forms;
    using System.Drawing;
    
    #endregion

    #region Class WorkflowMenuCommandService
    internal sealed class WorkflowMenuCommandService : MenuCommandService
    {
        public WorkflowMenuCommandService(IServiceProvider serviceProvider)
            : base(serviceProvider)
        {
            DeleteMenu = new ContextMenu();
            MenuItem mnuDelete = new MenuItem("&Delete", new EventHandler(OnMenuClicked));
            mnuDelete.Tag = "Delete";
            DeleteMenu.MenuItems.Add(mnuDelete);

            AddMenu = new ContextMenu();
            //MenuItem mnuAddStatus = new MenuItem("&Status", new EventHandler(OnMenuClicked));
            //mnuAddStatus.Tag = "AddStatus";

            MenuItem mnuAddReview = new MenuItem("&Review", new EventHandler(OnMenuClicked));
            mnuAddReview.Tag = "AddReview";

            MenuItem mnuAddSendMail = new MenuItem("&Send Mail", new EventHandler(OnMenuClicked));
            mnuAddSendMail.Tag = "AddSendMail";

            //MenuItem mnuAddIfElse = new MenuItem("&IfElse", new EventHandler(OnMenuClicked));
            //mnuAddIfElse.Tag = "AddIfElse";

            MenuItem mnuAdd = new MenuItem("&Add", new MenuItem[] {  mnuAddReview, mnuAddSendMail});
            mnuAdd.Tag = "";

            AddMenu.MenuItems.Add(mnuAdd);
        }

        private ContextMenu DeleteMenu;
        private ContextMenu AddMenu;

        public event EventHandler MenuClicked;

        public override void ShowContextMenu(CommandID menuID, int x, int y)
        {
            if (menuID == WorkflowMenuCommands.SelectionMenu)
            {
                ContextMenu contextMenu = null ;

                ISelectionService selectionService = GetService(typeof(ISelectionService)) as ISelectionService;
                if (selectionService != null)
                {
                    if (selectionService.PrimarySelection is Activity)
                    {
                        contextMenu = DeleteMenu;
                    }
                    else
                    {
                        contextMenu = AddMenu;
                    }
                }

                if (contextMenu != null)
                {
                    WorkflowView workflowView = GetService(typeof(WorkflowView)) as WorkflowView;
                    if (workflowView != null)
                        contextMenu.Show(workflowView, workflowView.PointToClient(new Point(x, y)));
                }
            }
        }

        private void OnMenuClicked(object sender, EventArgs e)
        {
            MenuItem menuItem = sender as MenuItem;
            if (menuItem != null )
            {
                if (MenuClicked != null)
                {
                    MenuClicked(sender, e);
                }   
            }
        }
    }
    #endregion
}